<?php

namespace app\common\model;


class ProductSpec extends BaseModel
{
    protected $table = 'product_spec';
    public function goods()
    {
        return $this->belongsTo('\app\common\model\Product', 'goods_id')->bind(['name','image']);
    }
    public static function updateSpec($goods_id, $insert_data)
    {

        return self::handleDbTrans(function() use ($goods_id, $insert_data){
            self::where('goods_id', $goods_id)->update(['is_del' => 1]);
            foreach ($insert_data as $k => $v){
                $v['is_default'] = ($k == 0) ? 1 : 0;
                if($v['id']) {
                    $v['is_del'] = 0;
                    self::where('id', $v['id'])->update($v);
                } else {
                    unset($v['id']);
                    self::insert($v);
                }
            }
        });
    }
}